package cn.gogoyc.user.controller;

import cn.gogoyc.org.basic.exception.BusinessException;
import cn.gogoyc.org.basic.util.AjaxResult;
import cn.gogoyc.user.dto.LoginDto;
import cn.gogoyc.user.service.ILogininfoService;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 前台用户登录接口
 * @author Yang
 * @date 2023 /2/10 12:17
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * Account ajax result.
     *
     * @param loginDto the login dto
     * @return the ajax result
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto) {
        try {
            //将dto数据进行处理返回HashMap存储
            Map<String, Object> map = logininfoService.account(loginDto);
            System.out.println(map);
            return AjaxResult.me().setObj(map);
        } catch (BusinessException e) {
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("系统繁忙...请稍后再试...", false);
        }
    }

    @PostMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code) {
        try {
            return logininfoService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("系统繁忙...请稍后再试...", false);
        }
    }

    @PostMapping("/wechat/binder")             //接收这个UUID
    public AjaxResult wechatLogin(@RequestBody Map<String, String> map) {
        try {
            return logininfoService.wechatBinder(map);
        } catch (BuilderException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        }
    }
}
